package com.fingard.dsp.bank.directbank.cmb03;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.text.StringHelper;

public class CMB03Base extends DirectBase {
	/**
	 * 获取字符编码
	 * 
	 * @return 当前字符编码
	 */
	protected String getCharset() {
		return getBankConfig().getCharset("utf-8");
	}

	/**
	 * 发送并获取返回
	 * 
	 * @param actItem
	 *            商户配置信息
	 * @param bizContent
	 *            业务JSON
	 * @param serviceCode
	 *            当前服务
	 * @return 响应JOSN
	 * @throws Exception
	 *             服务器请求异常
	 */
	public String sendToBank(JSONObject tmpReqJson, ActSetItem actItem) throws Exception {
		String sReturn = "";
		
		String tmpReqMsg = JSON.toJSONString(tmpReqJson);
		WriteBankLogStep2(tmpReqMsg);
		
		StringBuilder signData = new StringBuilder();
		signData.append(tmpReqMsg).append("&");
		signData.append(actItem.merchantId).append("&");
		String signTime = Format.DateTime14Format.format(new Date());
		signData.append(signTime).append("&");
		signData.append(actItem.saltKey);
		WriteBankLogLn("待签名原始数据:\r\n" + signData.toString());
		
		MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
		String charset = getCharset();
		messageDigest.update(signData.toString().getBytes(charset));
		byte byteBuffer[] = messageDigest.digest();
		String sign = StringHelper.bytesToHexString(byteBuffer).toUpperCase();
		WriteBankLogLn("签名结果:" + sign);
		
		WriteBankUrlLogLn(bankFront.serverURL);
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httppost = new HttpPost(bankFront.serverURL);
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("jsonRequestData", tmpReqMsg));
		formparams.add(new BasicNameValuePair("merchantNo",	actItem.merchantId));
		formparams.add(new BasicNameValuePair("signTime", signTime));
		formparams.add(new BasicNameValuePair("sign", sign));
		UrlEncodedFormEntity uefEntity;
		CloseableHttpResponse response = null;
		try {
			uefEntity = new UrlEncodedFormEntity(formparams, charset);
			httppost.setEntity(uefEntity);
			response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				sReturn = EntityUtils.toString(entity, charset);
				WriteBankLogStep3(sReturn);
			} else {
				WriteBankLogStep3(sReturn);
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			try {
				if (httpclient != null) {
					httpclient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sReturn;
	}
}